


# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
class Solution:
    def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        res = []
        def traverse(root):
            if root is None:
                return 
            res.append(root.val)
            traverse(root.left)
            traverse(root.right)
        traverse(root)
        return res
    
    def preorderTraversal2(self, root: Optional[TreeNode]) -> List[int]:
        res = []
        if not root:
            return res 
        res.append(root.val)
        res.extend(self.preorderTraversal2(root.left))
        res.extend(self.preorderTraversal2(root.right))

        return res

        
        